import cn.edu.dgut.Javabean_object.Person;
import cn.edu.dgut.Javabean_object.Person2;
import cn.edu.dgut.Javabean_object.userinfo;
import cn.edu.dgut.function.readfile;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/saveServlet")
public class saveServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Map<String,String> infoMap = (Map<String, String>) request.getSession().getAttribute("Login");
        String user_number = infoMap.get("user_number");
        //根据工号查找用户
        userinfo user = new readfile().localinfo(user_number);
        String name = request.getParameter("name");
        String phone_number = request.getParameter("phone_number");
        String faculty_title = request.getParameter("faculty_title");
        String group = request.getParameter("group");
        String qq= request.getParameter("qq");
        String weixin = request.getParameter("weixin");
            if (name.trim().equals("null")) {
                name = null;
            }
            if (phone_number.trim().equals("null")) {
                phone_number = null;
            }

        if (qq.trim().equals("null")) {
            qq = null;
        }

        if (weixin.trim().equals("null")) {
            weixin = null;
        }
        if(faculty_title.trim().equals("null")){
            faculty_title=null;
        }
        if(group.trim().equals("null")){
            group=null;
        }
      /*  user.setName(name);
        user.setPhone_number(phone_number);
        user.setQq(qq);
        user.setWeixin(weixin);
        new readfile().resetUserInfo(user);//写入文件中

        */

        //写入数据库
        EntityManager em = (EntityManager) request.getAttribute("_entity.manager");
        Person userer=em.find(Person.class,user_number);
        userer.setName(name);
        userer.setPhone_number(phone_number);
        userer.setQq(qq);
        userer.setWeixin(weixin);

        em.persist(userer);
        System.out.println("sdadadadadada");
        System.out.println(userer.getPhone_number());
        response.sendRedirect("/userInfo.jsp");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
